﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class tabTracking : System.Web.UI.UserControl
    {
        private SalesManagerEntities _context;
        private tblCustomer _customer;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public new void Load(SalesManagerEntities context, tblOrderHeader orderHeader, bool forceUpdate)
        {
            _context = context;
            _customer = orderHeader.Customer;

            if (!IsPostBack || forceUpdate || (Request.Params["__EVENTARGUMENT"] == "UpdateDetails"))
            {
                ListViewTracking.DataSource = (from c in _context.tblPackages.Local
                                              where c.InventoryTransactions.Any(t => t.OrderID == orderHeader.OrderID)
                                              select c).ToArray();
                ListViewTracking.DataBind();
            }
        }

        protected void ListViewInventoryTransactionDetail_DataBound(object sender, EventArgs args)
        {
            ListView lv = (ListView)sender;
            Literal ph = (Literal)lv.FindControl("plcTransactionDetailsHeader");
            if (ph != null)
                foreach (tblMiscDef miscDef in _context.tblMiscDef.Local.Where(c => c.TableName.Trim() == "tblInventoryTransactionDetail").OrderBy(c => c.FieldNumber))
                    ph.Text += "<td>" + HttpUtility.HtmlEncode(miscDef.Name) + "</td>";
        }

        protected string InventoryTransactionDetailRow(int index, object value)
        {
            tblMiscDef miscDef = _context.tblMiscDef.Local.Where(c => c.TableName.Trim() == "tblInventoryTransactionDetail" && c.FieldNumber == index).FirstOrDefault();
            if (miscDef != null)
                return "<td>" + HttpUtility.HtmlEncode(value.ToString()) + "</td>";
            return string.Empty;
        }
    }
}